//
//  WYCSetPassController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCZhifusetController.h"
#import "UIButton+CountDown.h"
#import "passwordView.h"
#import "configuration.h"
#import "WQCMakePlanViewController.h"
@interface WYCZhifusetController ()
@property(strong,nonatomic)NSString  *pass;
/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;
@end

@implementation WYCZhifusetController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WYCGlobalBg;

    self.title = @"设置交易密码";
    UIView  *payView =[[UIView alloc]initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, 100)];
   
    payView.userInteractionEnabled =YES;
    payView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:payView];
    
    UILabel *payLb = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth-20, 20)];
    payLb.text =@"请设置支付密码，用于支付验证";
    payLb.backgroundColor = [UIColor clearColor];
    payLb.textColor = [UIColor lightGrayColor];
    payLb.textAlignment = NSTextAlignmentLeft;
    payLb.font = [UIFont systemFontOfSize:13];
    [payView addSubview:payLb];
    
  
    
    passwordView *pdView = [[passwordView alloc] initWithFrame:CGRectMake(20, 35, self.view.bounds.size.width-20, 60)];
    pdView.returnPasswordStringBlock = ^(NSString *password){
        NSLog(@"支付密码%@",password);
        self.pass = password;

    };
    [payView addSubview:pdView];
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(30, CGRectGetMaxY(pdView.frame) + 400 * px, ScreenWidth-60, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 3;
    [loginButton setTitle:@"确认" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(setpasswordClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
}

-(void)setpasswordClick
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    if(self.pass.length == 0)
    {
        [SVProgressHUD showImage:nil status:@"交易密码不能为空"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString *pas = [RSA encryptString:self.pass publicKey:RSA_P_KEY];
    
    params[@"payPassword"] =pas;
    params[@"mobile"] =[WYCAccountTool unarchivemobile];
    params[@"code"] =self.codeStr;

    NSLog(@"穿密码%@",params);
    [HttpTool POST:updatePayPassword params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"修改交易密码%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [SVProgressHUD showImage:nil status:@"设置成功"];
            
            WYCAccount *account = [WYCAccountTool account];
            account.havePayPassword = @"0";
            [WYCAccountTool saveAccount:account];
            
            for (UIViewController *vc in self.navigationController.viewControllers) {
                if ([vc isKindOfClass:[WQCMakePlanViewController class]]) {
                    [self.navigationController popToViewController:vc animated:YES];
                    
                    return ;
                }
            }
            
             [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
         

        }else{
            [SVProgressHUD showImage:nil status:[responseObject objectForKey:@"message"]];

        }

    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:nil status:Failed];

    }];
    
}

@end

